import java.util.Arrays;

public class MyArrayList implements IArrayList{

    private int[] array ;
    private int usedsize;
    private static final int CAPACITY = 10;

    public MyArrayList() {
        this.array = new int[CAPACITY];
    }

    public boolean isFull(){
        /*if(usedsize == array.length){
            return true;
        }
        return false;*/
        return usedsize == array.length;
    }

    public void grow(){
        this.array = Arrays.copyOf(this.array,2*this.array.length);
    }

    @Override
    public void add(int data) {
        if(isFull()){
            //array = Arrays.copyOf(array,2*array.length);
            grow();
        }
        this.array[usedsize] = data;
        this.usedsize++;
    }

    public boolean posIsRight1(int pos){
        if(pos < 0 || pos > usedsize){
            return false;
        }
        return true;
    }

    private void posIsRight(int pos)throws PosException{
        if(pos < 0 || pos > usedsize){
            throw new PosException("Pos位置不合法！");
        }
    }

    @Override
    public void add(int pos, int data) {
        /*if(isFull()){
            array = Arrays.copyOf(array,2*array.length);
        }
        if(!posIsRight1(pos)){
            return;
        }
        for (int i = usedsize - 1; i >= pos; i--) {
            array[i + 1] = array[i];
        }

        array[pos] = data;
        usedsize++;*/

        try{
            posIsRight(pos);
            if(isFull()){
                grow();
            }
            for (int i = usedsize - 1; i >= pos; i--) {
                array[i + 1] = array[i];
            }

            array[pos] = data;
            usedsize++;

        }catch (PosException e){
            e.printStackTrace();
        }
    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < usedsize; i++) {
            if (array[i] == toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        int i = 0;
        for (; i < usedsize; i++) {
            if(array[i] == toFind){
                return i;
            }
        }
        return -1;
    }

    public boolean isEmpty(){
        return usedsize == 0;
    }

    private void checkEmpty() throws EmptyException{
        if(isEmpty()){
            throw new EmptyException("顺序表为空");
        }
    }

    @Override
    public int get(int pos) {
        /*if(!posIsRight1(pos)){
            return -1;
        }
        if (isEmpty()){
            return -1;
        }
        return array[pos];*/

        try {
            posIsRight(pos);
            checkEmpty();

            return array[pos];
        }catch (PosException | EmptyException e1){
            e1.printStackTrace();
        }
        return -1;
    }

    @Override
    public void set(int pos, int value) {
        /*if(!posIsRight1(pos)){
            return ;
        }
        if (isEmpty()){
            return ;
        }
        array[pos] = value;*/
        try {
            posIsRight(pos);
            checkEmpty();

            array[pos] = value;
        }catch (EmptyException | PosException e){
            e.printStackTrace();
        }
    }

    @Override
    public void remove(int toRemove) {
        /*if (isEmpty()){
            return ;
        }
        int pos = indexOf(toRemove);
        if(pos == -1){
            return;
        }
        for (int i = pos; i < usedsize; i++) {
            array[i] = array[i + 1];
        }
        usedsize--;*/

        try{
            checkEmpty();

            int pos = indexOf(toRemove);
            if(pos == -1){
                return;
            }
            for (int i = pos; i < usedsize; i++) {
                array[i] = array[i + 1];
            }
            usedsize--;
        }catch (EmptyException e){
            e.printStackTrace();
        }
    }

    @Override
    public int size() {
        return this.usedsize;
    }

    @Override
    public void clear() {
        this.usedsize = 0;
    }

    @Override
    public void display() {
        for (int i = 0; i < usedsize; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }
}
